/**
 * 
 */
package it.unisalento.appsalento.asynctasc;

import it.unisalento.R;
import it.unisalento.appsalento.Constants.Constants;
import it.unisalento.appsalento.activity.App;
import it.unisalento.appsalento.activity.fragment.LibrettoFragment;
import it.unisalento.appsalento.asynctask.framework.MIMETypes;
import it.unisalento.appsalento.bo.mock.MockBo;
import it.unisalento.appsalento.model.Esame;
import it.unisalento.appsalento.model.GetLibrettoResponse;
import it.unisalento.appsalento.model.Request;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.app.FragmentManager;
import android.content.Context;
import android.view.MenuItem;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

/**
 * @author AGM
 * 
 */
public class LibrettoAsyncTask extends SyncAsyncTask {
	public LibrettoAsyncTask(Context context, MenuItem refreshMenuItem) {
		this.context = context;
		this.refreshMenuItem = refreshMenuItem;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		if (refreshMenuItem != null) {
		if(App.librettoFragment != null && App.librettoFragment.isVisible()){
			App.librettoFragment = new LibrettoFragment();
			FragmentManager fragmentManager = App.currentActivity().getFragmentManager();
			fragmentManager.beginTransaction().replace(R.id.content_frame, App.librettoFragment).commit();
		}
		 refreshMenuItem.collapseActionView();
         refreshMenuItem.setActionView(null);
		}

	}


	/**
	 * 
	 * @param holder
	 * @param httpclient
	 * @param httpost
	 * @param mapper
	 * @return
	 * @throws JSONException
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 */
	public boolean setData(ObjectMapper mapper) throws JSONException,
			UnsupportedEncodingException, IOException, ClientProtocolException,
			JsonParseException, JsonMappingException {
		boolean retVal = false;
		try {
			GetLibrettoResponse librettoResponse = null;
			if(!Constants.MOCK_ENABLED){
				Request request = new Request();
				request.setUtente(App.utente);
				final Gson gson = new GsonBuilder().create();
				final Client client = Client.create();
			    final WebResource resource = client.resource(Constants.URL_REST + Constants.SERVICE_LIBRETTO);
			    final ClientResponse response = resource.type(MIMETypes.APPLICATION_JSON.getName()).post(ClientResponse.class, gson.toJson(request));
			    if (response.getStatus() != 201 && response.getStatus() != 200) {
			      throw new RuntimeException("failed: http error code = " + response.getStatus());
			    }
			    final String responseEntity = response.getEntity(String.class).replaceAll("\\\\", "");
			    librettoResponse = mapper.readValue(responseEntity, GetLibrettoResponse.class);
			}else{
				InputStream is = context.getAssets().open(MockBo.GET_LIBRETTO_RESPONSE);
				librettoResponse = mapper.readValue(is, GetLibrettoResponse.class);
			}
			if(librettoResponse != null){ 
				App.listaEsami = new ArrayList<Esame>();
				if (librettoResponse.isOk()) {
					App.listaEsami = librettoResponse.getListaEsami();
				}
				retVal = librettoResponse.isOk();
			}
		} catch (Throwable t) {
			App.listaEsami = new ArrayList<Esame>();
			t.printStackTrace();
		}
		return retVal;
	}
	
}
